package com.uinnova.product.eam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.binary.core.util.BinaryUtils;
import com.uinnova.product.eam.comm.model.es.EamDiagramRelationSys;
import com.uinnova.product.eam.dao.ChangeAssigneeRecordDao;
import com.uinnova.product.eam.dao.CommunicateRecordDao;
import com.uinnova.product.eam.dto.AssignessInfo;
import com.uinnova.product.eam.feign.workable.entity.TaskRequest;
import com.uinnova.product.eam.feign.workable.entity.TaskResponse;
import com.uinnova.product.eam.model.es.ChangeAssigneeRecord;
import com.uinnova.product.eam.model.es.CommunicateRecord;
import com.uinnova.product.eam.service.CommunicateRecordService;
import com.uinnova.product.eam.service.ICISwitchSvc;
import com.uinnova.product.eam.service.es.EamDiagramRelationSysDao;
import com.uinnova.product.vmdb.comm.model.ci.CCcCi;
import com.uinnova.product.vmdb.provider.ci.bean.CcCiInfo;
import com.uinnova.project.base.diagram.comm.model.ESDiagram;
import com.uinnova.project.service.eam.ESDiagramSvc;
import com.uino.bean.cmdb.base.ESCIInfo;
import com.uino.bean.cmdb.base.LibType;
import com.uino.dao.util.ESUtil;
import com.uino.util.sys.SysUtil;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CommunicateRecordServiceImpl implements CommunicateRecordService {

    @Autowired
    private CommunicateRecordDao communicateRecordDao;
    @Resource
    private ESDiagramSvc esDiagramSvc;
    @Resource
    private EamDiagramRelationSysDao eamDiagramRelationSysDao;
    @Autowired
    private IamsCISwitchSvc ciSwitchSvc;

    public long addRecord(AssignessInfo assignessInfo, TaskResponse taskResponse,Boolean isReback) {
        CommunicateRecord communicateRecord = new CommunicateRecord();
        communicateRecord.setId(ESUtil.getUUID());
        communicateRecord.setTaskId(assignessInfo.getTaskId());
        communicateRecord.setProcessInstanceId(taskResponse.getProcessInstanceId());
        communicateRecord.setReback(isReback);
        communicateRecord.setRemarks(assignessInfo.getRemarks());
        communicateRecord.setCommunicatePromoter(taskResponse.getUserId());
        communicateRecord.setCommunicateUser(assignessInfo.getUserId());
        communicateRecord.setCommunicateTime(ESUtil.getNumberDateTime());
        communicateRecordDao.saveOrUpdate(communicateRecord);
        return communicateRecord.getId();
    }

    @Override
    public void deleteById(Long id) {
        communicateRecordDao.deleteById(id);
    }

    @Override
    public List<CommunicateRecord> queryByProcessInstanceId(String processInstanceId) {
        TermQueryBuilder query = QueryBuilders.termQuery("processInstanceId.keyword", processInstanceId);
        List<CommunicateRecord> records = communicateRecordDao.getListByQueryScroll(query);
        records.sort(Comparator.comparing(CommunicateRecord::getCommunicateTime));
        return records;
    }

    @Override
    public CommunicateRecord getById(Long id) {
        return communicateRecordDao.getById(id);
    }

}
